/**
 * 处理评论的相关api
 * created by dqn 2018/3/14 19:11
 */

package main.controller;

import main.entity.Comment;
import main.entity.WebUser;
import main.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class CommentController {
    @Autowired
    CommentService commentService;
    /**
     * 获取某首歌的评论，可分页
     * @param id
     * @return [comment1,comment2,comment3,comment4...]
     */
//    @RequestMapping(value = "/comment/song/{id}/page/{page}/size/{size}",method = RequestMethod.GET)  @PathVariable int page,@PathVariable int size
    @RequestMapping(value = "/comments/song/{id}",method = RequestMethod.GET)
    public List<Comment> getCommentsBySongPk(@PathVariable int id){
        List<Comment> comments = commentService.getCommentsBySongPk(id);
        return comments.parallelStream()
                .sorted((a,b)-> (int) (b.getPublish().getTime()-a.getPublish().getTime()))
                .limit(20)
                .collect(Collectors.toList());
    }

    /**
     * 添加一条评论
     * @param comment
     * @return json {'result':'succeed/failed/permission error/error'}
     */
    @RequestMapping(value = "/comment/add",method = RequestMethod.POST)
    public Map<String,Object> addComment(Comment comment,HttpSession httpSession,int song){
        Map<String,Object> resultMap = new HashMap<>();
        String key = "result";
        final String SUCCEED = "succeed";
        final String FAILED = "FAILED";
        final String PERMISSION_ERROR = "permission error";
        WebUser webUser = (WebUser) httpSession.getAttribute("user");
        if (webUser==null){
            resultMap.put(key,PERMISSION_ERROR);
            return resultMap;
        }

        comment.setUser(webUser.getNumber());
        comment.setSong(song);
        comment.setPublish(new Date(System.currentTimeMillis()));
        Comment saved = commentService.addComment(comment);
        if (saved != null){
            resultMap.put(key,SUCCEED);
        }else {
            resultMap.put(key,FAILED);
        }
        resultMap.put("comment",saved);
        return resultMap;
    }

    /**
     * 根据评论的id来删除评论
     * @param id
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/comment/del/{id}",method = RequestMethod.GET)
    public Map<String,String> delCommentById(@PathVariable int id, HttpSession httpSession){
        Map<String,String> resultMap = new HashMap<>();
        String key = "result";
        WebUser webUser = (WebUser) httpSession.getAttribute("user");
        if (webUser==null || !webUser.getPermission().equals(WebUser.ADMINISTRATOR)){
            resultMap.put(key,"error");
            return resultMap;
        }
        if (commentService.delCommentByPk(id)){
            resultMap.put(key,"done");
            return resultMap;
        }else {
            resultMap.put(key,"error");
            return resultMap;
        }
    }

    @RequestMapping(value = "/comments/user",method = RequestMethod.GET)
    public List<Comment> getCommentsCrearedBy(String name){
        return commentService.getCommentsByUser(name);
    }
}
